package com.coin.exchanges.view.user

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.coin.exchanges.R
import com.coin.exchanges.net.RetrofitFactory
import com.coin.exchanges.presenter.TestPresenter
import com.coin.exchanges.utils.LCSharedPreferencesHelper
import com.coin.exchanges.utils.SpUtils
import com.coin.exchanges.view.user.adapter.PersonFlowAdapter
import com.coin.exchanges.view.user.bean.PersonFlow
import com.coin.exchanges.view.user.bean.UserLoginBean
import com.coin.libbase.view.activity.JToolbarActivity
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_personal_flow.*
import java.util.HashMap

class PersonalFlowActivity : JToolbarActivity<TestPresenter>() {
    override fun getLayout(): Int {
       return R.layout.activity_personal_flow
    }

    override fun initIntent(intent: Intent?) {
    }

    override fun initView() {
        val sharedPreferencesHelper = LCSharedPreferencesHelper.instance(this@PersonalFlowActivity, "register")

        val userId = sharedPreferencesHelper.getValue("UserId")
        PostPersonalFlow(userId)
    }

    override fun initData() {
    }

    private fun PostPersonalFlow(user_id: String){
        val map = HashMap<String, String>()
        map["user_id"] = user_id
        RetrofitFactory
                .getOkExApiService()
                .getFinance(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<PersonFlow>{
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable) {
                    }

                    override fun onError(e: Throwable) {
                    }

                    override fun onNext(t: PersonFlow) {
                        if(t.data.size>0){
                            noDataLl.visibility= View.GONE
                            mPersonFlowRv.visibility= View.VISIBLE
                            mPersonFlowRv.layoutManager = LinearLayoutManager(this@PersonalFlowActivity)
                            val mAdapter = PersonFlowAdapter(R.layout.item_person_flow, t.data)
                            mPersonFlowRv.adapter = mAdapter
                        }else{
                            noDataLl.visibility= View.VISIBLE
                            mPersonFlowRv.visibility= View.GONE
                        }

                    }

                })
    }
}
